home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 15 / develop 15 code / Symmetry & Tiles / Tiler Code / utils.c < prev   
Encoding:
C/C++ Source or Header  |  1993-09-13  |  3.4 KB  |  137 lines  |  [TEXT/KAHL]

  1. #include     "Shell.h"
  2. #include     <Traps.h>
  3.  
  4. extern  Boolean        doneflag;
  5.  
  6. /* These are stolen from arith.c, so we don't have to include the ANSI library */
  7. int
  8. abs(int i)
  9. {
  10.     if (i < 0)
  11.         return(-i);
  12.     return(i);
  13. }
  14.  
  15.  
  16. long
  17. labs(long i)
  18. {
  19.     if (i < 0)
  20.         return(-i);
  21.     return(i);
  22. }
  23.  
  24.  
  25. /****************************************************
  26. Quickdraw Things...
  27. *****************************************************/
  28.  
  29. /*------------------------------------------------------------------------
  30. NewPixImage()            Creates pix image and inits the fields of the pixmap...
  31. ------------------------------------------------------------------------*/
  32.  
  33. Boolean NewPixImage(PixMapHandle ThePix, Rect *TheRect, short dpth)
  34. {
  35.     Ptr                    ThePtr;
  36.     long                Offrowbytes, ptrsize;
  37.     
  38.     Offrowbytes    =(((dpth * (TheRect->right - TheRect->left)) + 15) / 16) * 2;
  39.     ptrsize = (TheRect->bottom - TheRect->top) * Offrowbytes;
  40.     ThePtr = NewPtr(ptrsize);
  41.     if(MemError() != noErr)
  42.         return(false);
  43.     
  44.     (**ThePix).baseAddr = ThePtr;
  45.     (**ThePix).rowBytes = Offrowbytes + 0x8000;
  46.     (**ThePix).bounds = *TheRect;
  47.     (**ThePix).pixelSize = dpth;
  48.     (**ThePix).cmpCount = 1;
  49.     (**ThePix).cmpSize = dpth;
  50.     return(true);
  51. }
  52.     
  53.  
  54. /*------------------------------------------------------------------------
  55. NewBitMap()            Creates bit image and inits the fields of the bitmap...
  56.  
  57. Boolean NewBitMap(BitMap *TheMap, Rect *TheRect)
  58. {
  59.     long    rb;
  60.     Ptr        ba;
  61.     
  62.     rb = ((TheRect->right - TheRect->left + 15) / 16) * 2;
  63.     ba = NewPtr(rb * (TheRect->bottom - TheRect->top));
  64.     if( MemError() == noErr)
  65.     {
  66.         TheMap->rowBytes = rb;
  67.         TheMap->baseAddr = ba;
  68.         TheMap->bounds = *TheRect;
  69.         return(true);
  70.     }
  71.     else
  72.         return(false);
  73. }
  74. ------------------------------------------------------------------------*/
  75.  
  76.  
  77. /*-------------------------------------------------------------------------
  78. CenterRect()             Centers theRect over thePt...
  79. --------------------------------------------------------------------------*/
  80. void CenterRect(Rect *theRect, Point *thePt)
  81. {
  82.     /* First home theRect... */
  83.     
  84.     OffsetRect(theRect, -theRect->left, -theRect->top);
  85.     
  86.     /* ...then center it over thePt */
  87.     
  88.      
  89.     thePt->h = thePt->h - (theRect->right / 2);
  90.     thePt->v = thePt->v - (theRect->bottom / 2);
  91.     OffsetRect(theRect, thePt->h, thePt->v);
  92. }
  93.  
  94. /*-------------------------------------------------------------------------
  95. Center()             Returns the center of the rect
  96. --------------------------------------------------------------------------*/
  97. Point Center(Rect *theRect)
  98. {
  99.     Point        pt;
  100.         
  101.     pt.h = theRect->left + ((theRect->right - theRect->left) / 2);
  102.     pt.v = theRect->top + ((theRect->bottom - theRect->top) / 2);
  103.     return(pt);
  104. }
  105.  
  106.  
  107. /*-------------------------------------------------------------------------
  108. These routines are from DTS Utilities
  109. --------------------------------------------------------------------------*/
  110. short    NumToolboxTraps(void)
  111. {
  112.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  113.         return (0x200);
  114.     else
  115.         return (0x400);
  116. }
  117.  
  118. TrapType    GetTrapType(short theTrap)
  119. {
  120.     /* OS traps start with A0, Tool with A8 or AA. */
  121.     if ((theTrap & 0x0800) == 0)                    /* per D.A. */
  122.         return (OSTrap);
  123.     else
  124.         return (ToolTrap);
  125. }
  126.  
  127. Boolean TrapAvailable(short theTrap)
  128. {
  129.     TrapType    theTrapType;
  130.  
  131.     theTrapType = GetTrapType(theTrap);
  132.     if ((theTrapType == ToolTrap) && ((theTrap &= 0x07FF) >= NumToolboxTraps()))
  133.         theTrap = _Unimplemented;
  134.  
  135.     return (NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(theTrap,
  136.                   theTrapType));
  137. }